//app.js
App({
  onLaunch: function () {
   
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // wx.navigateTo({
        //   url: '../index/index',
        // })
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: {
    userInfo: null,
    action: 'new',
    openid: null,
    indexUrl: "/pages/getTick/index/index",
    host: "www.diaochami.com",
    jssessionid: "",
    launchFlag: false
  },
  /**
* 用户登录
*/
  loginmini: function (callback) {
    var that = this;
    //调用登录接口
    let host = 'https://' + that.globalData.host;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: host + '/wxss/login.do',
            method: "POST",
            data: {
              code: res.code,
              origin: "huodong"
            },
            success: function (res) {
              if (res.statusCode == 200) {
                console.log('b5');
                // res.data.needFillUpMessage = true
                if (res.data.needFillUpMessage == true) {
                  wx.setStorageSync('re_session', res.data.sessionId);
                  wx.reLaunch({
                    url: "/pages/first/first"
                  })
                  // callback('need');
                } else {
                  wx.setStorageSync('JSSESSIONID', res.data.sessionId);
                  callback('no');
                }
              } else {
                console.log("login接口异常不是200");
              }
            },
            fail: function (res) {
              that.globalData.launchFlag = true;
              console.log('获取用户登录态失败！' + res.errMsg)
            }

          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })

  },
  /**
  * 登录获取code 
  */
  _login: function (options) {
    var that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          that._sendCode(res.code, options);
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      },
      fail: function (res) {
        console.log('login fail: ' + res.errMsg);
      }
    })
  }
})